package org.zhangxiao.paladin2.admin.shiro;

import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 管理员权限内存缓存
 * 在AdminRealm中获取用户授权数据的时候使用，如果存在，则不重新获取
 */
public class AdminAuthorizationInfoStorageRedis implements AdminAuthorizationInfoStorage {

    private static final String REDIS_KEY_PREFIX = "shiro-storage:";
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public SimpleAuthorizationInfo get(Long adminId) {
        return (SimpleAuthorizationInfo) redisTemplate.opsForValue().get(getRedisKey(adminId));
    }

    @Override
    public void put(Long adminId, SimpleAuthorizationInfo authorizationInfo) {
        redisTemplate.opsForValue().set(getRedisKey(adminId), authorizationInfo);
    }

    @Override
    public void remove(Long adminId) {
        redisTemplate.delete(getRedisKey(adminId));
    }

    private String getRedisKey(Long dbKey) {
        return REDIS_KEY_PREFIX + dbKey;
    }
}
